package aeduclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Adrian Hostettler
 */
class NameClientConn extends Observable
        implements Runnable {

    private Socket nameSocket=null;
    private int playerId;
    private String playerName;
    private BufferedReader inFromServer;
    private PrintWriter outToServer;

    /**
     * Konstruktor, baut Datenstroeme zum/vom Server auf.
     *
     * @param socket Socket fuer Serververbindung
     */
    public NameClientConn(Socket socket){
        try {
            this.nameSocket = socket;
            outToServer = new PrintWriter(nameSocket.getOutputStream(), true);
            inFromServer = new BufferedReader(new InputStreamReader(nameSocket.getInputStream()));
// Thread
            this.run();
        } catch (Exception e) {
            System.out.println("\nnameClient conn konnte nicht gestartet werden\n  " + e);
        }
    }

    /**
     * Interaktion, liest und verarbeitet Nachrichten vom Server.
     */
    @Override
    public final void run() {
         BufferedReader vonTastatur = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Name:");
        try {
            playerName = vonTastatur.readLine();
            outToServer.println(playerName);
        } catch (Exception e) {
            try {
                System.out.println("\nVerbindung zum Server unterbrochen\n " + e);
                nameSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(NameClientConn.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            playerId = Integer.parseInt(inFromServer.readLine());
            System.out.println(playerId);
            setChanged();
            notifyObservers();
        } catch (IOException e) {
            try {
                System.out.println("\nVerbindung zum Server unterbrochen\n  " + e);
                nameSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(NameClientConn.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Nutzereingaben, Eingabe und Versenden von Nachrichten.
     */
    public void sendNameToServer() {
        BufferedReader vonTastatur = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Name:");
        try {
            playerName = vonTastatur.readLine();
            outToServer.println(playerName);
        } catch (Exception e) {
            try {
                System.out.println("\nVerbindung zum Server unterbrochen\n " + e);
                nameSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(NameClientConn.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public int getPlayerId() {
        return playerId;
    }
}
